<template>
  <div class="main2">
    <van-list v-model="loading" :finished="finished" @load="onLoad">
      <ul class="msgUserList">
        <li
          @click="$router.push('/user/msg?FromUserID='+item.ID+'&UserID='+$store.state.userInfo.ID)"
          v-for="(item,index) in userList"
          :key="index"
        >
          <img :src="item.HeadimgUrl" alt>
          <div>
            <h4>{{item.NickName}}</h4>
            <h2>{{item.lastMsg}}</h2>
            <b v-show="item.msgNum">{{item.msgNum}}</b>
          </div>
        </li>
      </ul>
    </van-list>
    <div class="info" v-show="userList.length==0">暂无信息</div>
  </div>
</template>

<script>
import api from "@/api/api";
export default {
  data() {
    return {
      userList: [],
      finished: true,
      loading: false,
      timer:null
    };
  },
  methods: {
    getUserList() {
      api.GetChatUser({ UserID: this.$store.state.userInfo.ID }).then(res => {
        this.userList = res;
      });
    },
    onLoad() {
      api.GetChatUser({ UserID: 92 }).then(res => {
        res.forEach(n => {
          this.userList.push(n);
        });
      });
    }
  },
  created() {
    var that = this;
    this.timer = setInterval(function() {
      if (that.$store.state.userInfo.ID) {
        that.getUserList();
        clearInterval(that.timer);
      }
    }, 300);
   }
};
</script>

<style scoped lang='less'>
.main2 {
  .info {
    margin: 50px auto;
    color: #999;
  }
  .msgUserList {
    li {
      width: 100%;
      position: relative;
      padding-left: 90px;
      padding-top: 5px;
      text-align: left;
      border-bottom: 1px solid #d9d9d9;
      min-height: 75px;

      img {
        position: absolute;
        width: 60px;
        height: 60px;
        left: 10px;
        top: 10px;
        border-radius: 10px;
      }
      h4 {
        font-size: 16px;
        color: #596b91;
      }
      h2 {
        font-size: 14px;
        overflow: hidden;
        text-overflow: ellipsis;
        max-height: 4em;
        color: #999;
        margin-top: 5px;
      }
      b {
        position: absolute;
        border-radius: 50%;
        width: 1.5em;
        height: 1.5em;
        line-height: 1.5em;
        background: #ff3f3e;
        color: #fff;
        text-align: center;
        font-weight: normal;
        left: 55px;
        top: 5px;
        font-size: 14px;
      }
    }
  }
}
</style>